package com.gec.zhs.controller;

import com.gec.zhs.annotation.ZhsLogin;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.dto.UserMessageCountDto;
import com.gec.zhs.entity.enums.MessageReadTypeEnum;
import com.gec.zhs.entity.pojo.UserMessage;
import com.gec.zhs.entity.pojo.UserVideoSeries;
import com.gec.zhs.entity.query.UserMessageQuery;
import com.gec.zhs.entity.vo.PaginationResultVO;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.service.UserMessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-03-05
 * @description
 */
@RestController
@RequestMapping("/message")
public class UserMessageController extends ABaseController{

    @Autowired
    private UserMessageService userMessageService;

    @ApiOperation("未读消息的数量")
    @RequestMapping("/getNoReadCount")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getNoReadCount() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setUserId(tokenUserInfoDto.getUserId());
        userMessageQuery.setReadType(MessageReadTypeEnum.NO_READ.getType());
        Integer count = userMessageService.findCountByParam(userMessageQuery);
        return getSuccessResponseVO(count);
    }

    @ApiOperation("未读消息数量分组")
    @RequestMapping("/getNoReadCountGroup")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getNoReadCountGroup() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        List<UserMessageCountDto> messageTypeNoReadCountList = userMessageService.getMessageTypeNoReadCount(tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(messageTypeNoReadCountList);
    }

    @ApiOperation("点击阅读消息")
    @RequestMapping("/readAll")
    @ZhsLogin(checkLogin = true)
    public ResponseVO readAll(Integer messageType) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setUserId(tokenUserInfoDto.getUserId());
        userMessageQuery.setMessageType(messageType);

        UserMessage userMessage = new UserMessage();
        userMessage.setReadType(MessageReadTypeEnum.READ.getType());

        userMessageService.updateByParam(userMessage,userMessageQuery);

        return getSuccessResponseVO(null);
    }

    @ApiOperation("点击消息的时候加载消息内容")
    @RequestMapping("/loadMessage")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadMessage(Integer messageType,Integer pageNo) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserMessageQuery messageQuery = new UserMessageQuery();
        messageQuery.setMessageType(messageType);
        messageQuery.setPageNo(pageNo);
        messageQuery.setUserId(tokenUserInfoDto.getUserId());
        messageQuery.setOrderBy("message_id desc");
        PaginationResultVO<UserMessage> page = userMessageService.findListByPage(messageQuery);
        return getSuccessResponseVO(page);
    }


    @ApiOperation("删除消息")
    @RequestMapping("/delMessage")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delMessage(Integer messageId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setUserId(tokenUserInfoDto.getUserId());
        userMessageQuery.setMessageId(messageId);

        userMessageService.deleteByParam(userMessageQuery);
        return getSuccessResponseVO(null);
    }
}
